﻿@inherits BaseComponent
@typeparam TItem where TItem : class, new()
@{
    var totalWidth = Model.Columns.Where(c => c.IsVisible).Select(c => c.Width > 0 ? c.Width : 100).Sum();
    var scrollX = totalWidth.ToString();
}
<ReloadContainer @ref="reload">
    <AntTable @ref="table" TItem="TItem" Context="Context" Model="Model" ScrollX="@scrollX" ScrollY="@ScrollY" 
              ExpandTemplate="ExpandTemplate" OnExpand="OnExpand" OnRow="OnRow" OnChange="OnChange"
              Total="totalCount" DataSource="dataSource" Loading="Model.EnableSpin && isQuering"
              @bind-SelectedRows="Model.SelectedRows">
        <ColumnDefinitions>
            @if (Context.IsEditTable) {
                <TableColumn TitleTemplate="ColumnTitle" Width="40" Align="ColumnAlign.Center" Fixed="ColumnFixPlacement.Left" />
            }
            @if (Model.SelectType != TableSelectType.None) {
                <Selection Type="@GetSelectionType(Model.SelectType)" Align="ColumnAlign.Center" Width="40" Fixed="ColumnFixPlacement.Left" />
            }
            @if (Model.Columns != null && Model.Columns.Count > 0) {
                var isDictionary = Model.IsDictionary;
                if (Model.ShowIndex) {
                    var columnId = Model.Columns[0].Id;
                    if (isDictionary) {
                        @*<DictionaryColumn Title="@Language.Index" Property="c=>c.GetValue(columnId)" Align="ColumnAlign.Center" Width="50" Fixed="ColumnFixPlacement.Left">
                            <span>@GetIndex(context)</span>
                        </DictionaryColumn>*@
                    } else {
                        <TableColumn Title="@Language.Index" Align="ColumnAlign.Center" Width="50" Fixed="ColumnFixPlacement.Left">
                            <span>@GetIndex(context)</span>
                        </TableColumn>
                    }
                }
                var columns = GetColumns();
                foreach (var item in columns) {
                    if (string.IsNullOrWhiteSpace(item.Id))
                        continue;

                    var title = Language?.GetFieldName<TItem>(item);
                    var width = item.Width > 0 ? item.Width.ToString() : "";
                    RenderFragment<TItem> template = null;
                    Model.Templates?.TryGetValue(item.Id, out template);
                    if (isDictionary) {
                        var data = context as Dictionary<string, object>;
                        var value = data?.GetValue(item.Id);
                        <DictionaryColumn Title="@title" TitleTemplate="@DataTitle(item, title)" Property="c=>c.GetValue(item.Id)" Width="@width"
                                          Ellipsis="item.Ellipsis" Fixed="@item.ToColumnFixPlacement()" Align="@item.ToColumnAlign()"
                                          ShowSorterTooltip="false" Sortable="GetSortable(item)" DefaultSortOrder="item.ToSortDirection()"
                                          ColSpan="@GetRowSpan(item, value)" RowSpan="@GetColSpan(item, value)"
                                          FilterDropdown="@GetFilterTemplate(item)">
                            @if (template != null) {
                                @template(context)
                            } else {
                                @RenderCell(context, item, value)
                            }
                        </DictionaryColumn>
                    } else {
                        if (typeof(TItem).HasProperty(item.Id)) {
                            var value = TypeHelper.GetPropertyValue(context, item.Id);
                            <Column Title="@title" TitleTemplate="@DataTitle(item, title)" DataIndex="@item.Id" TData="object" Width="@width"
                                    Ellipsis="item.Ellipsis" Fixed="@item.ToColumnFixPlacement()" Align="@item.ToColumnAlign()"
                                    ShowSorterTooltip="false" Sortable="GetSortable(item)" DefaultSortOrder="item.ToSortDirection()"
                                    ColSpan="@GetRowSpan(item, value)" RowSpan="@GetColSpan(item, value)"
                                    FilterDropdown="@GetFilterTemplate(item)">
                                @if (template != null) {
                                    @template(context)
                                } else {
                                    @RenderCell(context, item, value)
                                }
                            </Column>
                        } else {
                            <Column Title="@title" TitleTemplate="@DataTitle(item, title)" TData="object" Width="@width"
                                    Ellipsis="item.Ellipsis" Fixed="@item.ToColumnFixPlacement()" Align="@item.ToColumnAlign()"
                                    ShowSorterTooltip="false" Sortable="GetSortable(item)" DefaultSortOrder="item.ToSortDirection()"
                                    ColSpan="@GetRowSpan(item, item.Id)" RowSpan="@GetColSpan(item, item.Id)"
                                    FilterDropdown="@GetFilterTemplate(item)">
                                @if (template != null) {
                                    @template(context)
                                } else {
                                    @RenderCell(context, item, item.Id)
                                }
                            </Column>
                        }
                    }
                }
            }
            @if (Model.HasAction) {
                <ActionColumn TitleTemplate="ActionTitle" Width="@Model.ActionWidth" Align="ColumnAlign.Center" Fixed="ColumnFixPlacement.Right">
                    @RenderAction(context)
                </ActionColumn>
            } else if (Context.IsEditTable) {
                <ActionColumn TitleTemplate="ActionTitle" Width="@Model.ActionWidth" Align="ColumnAlign.Center" Fixed="ColumnFixPlacement.Right" />
            }
            @if (Model.HasSum) {
                @RenderSummary()
            }
        </ColumnDefinitions>
    </AntTable>
</ReloadContainer>

@code {
    RenderFragment ActionTitle => @<span>
        <span>@Language[Language.Action]</span>
        @if (Context.IsEditTable) {
            <Tooltip Title="@Language[Language.AddAction]" Class="kui-edit" Style="margin-left:10px;display:inline-block;">
                <KIcon Icon="plus" OnClick="this.Callback<MouseEventArgs>(e => OnAddAction())" />
            </Tooltip>
        }
    </span>;

    RenderFragment ColumnTitle => @<Tooltip Title="@Language[Language.AddColumn]" Class="kui-edit">
        <KIcon Icon="plus" OnClick="this.Callback<MouseEventArgs>(e => OnAddColumn())" />
    </Tooltip>;

    RenderFragment DataTitle(ColumnInfo item, string title) => @<span>
        @if (!string.IsNullOrWhiteSpace(item.Tooltip)) {
            <Tooltip Title="@item.Tooltip">@Language[title]</Tooltip>
        } else {
            @Language[title]
        }
    </span>;

    RenderFragment RenderCell(TItem context, ColumnInfo item, object value) => @<span>
        @if (item.Type == FieldType.Switch) {
            var isChecked = Utils.ConvertTo<bool>(value);
            <Switch Disabled Value="isChecked" />
        } else if (item.Type == FieldType.File) {
            <KFileCell Value="@value?.ToString()" />
        } else {
            var text = GetColumnText(item, value);
            if (item.IsViewLink) {
                <KLink OnClick="e=>OnViewForm(context)">@text</KLink>
            } else if (item.LinkAction != null) {
                <KLink OnClick="e=>OnLinkAction(item, context)">@text</KLink>
            } else {
                <span>@text</span>
            }
        }
    </span>;

    RenderFragment RenderAction(TItem context) => @<span>
        @{
            var actions = Model.RowActions?.Invoke(context);
            if (actions == null)
                actions = Model.Actions;
            Model.UpdateRowActions?.Invoke(context, actions);
            actions = actions?.Where(a => a.Visible).ToList();
        }
        @if (actions.Count > Model.ActionCount) {
            var takes = actions.Skip(0).Take(Model.ActionCount - 1).ToList();
            var mores = actions.Skip(Model.ActionCount - 1).ToList();
            foreach (var item in takes) {
                <KButton Name="@item.Name" Type="item.ToType()" Danger="item.IsDanger()" OnClick="this.Callback<MouseEventArgs>(e=>OnActionClick(item, context))" />
            }
            var model = new DropdownModel {
                Text = Language.More,
                Items = mores,
                OnItemClick = e => OnActionClick(e, context)
            };
            <AntDropdown Context="Context" Model="model" />
        } else {
            foreach (var item in actions) {
                <KButton Name="@item.Name" Type="item.ToType()" Danger="item.IsDanger()" OnClick="this.Callback<MouseEventArgs>(e=>OnActionClick(item, context))" />
            }
        }
    </span>;

    RenderFragment RenderSummary() => @<SummaryRow>
        @if (Context.IsEditTable) {
            <SummaryCell />
        }
        @if (Model.SelectType != TableSelectType.None) {
            <SummaryCell />
        }
        @if (Model.ShowIndex) {
            <SummaryCell />
        }
        @if (Model.Columns != null && Model.Columns.Count > 0) {
            var isTitle = false;
            var columns = GetColumns();
            foreach (var item in columns) {
                if (string.IsNullOrWhiteSpace(item.Id))
                    continue;

                if (!isTitle) {
                    <SummaryCell>
                        @if (Model.ShowPager) {
                            @Language[Language.SumPage]<br/>
                        }
                        <strong>@Language[Language.SumQuery]</strong>
                    </SummaryCell>
                    isTitle = true;
                    continue;
                }

                if (item.IsSum) {
                    var value = Model.Result?.GetPageSum(item.Id);
                    var statis = Model.Result?.Statis?.First(s => s.Key.Equals(item.Id, StringComparison.OrdinalIgnoreCase));
                    var total = statis?.Value;
                    <SummaryCell>
                        @if (Model.ShowPager) {
                            @value<br/>
                        }
                        <strong>@total</strong>
                    </SummaryCell>
                } else {
                    <SummaryCell />
                }
            }
        }
        @if (Model.HasAction) {
            <SummaryCell />
        }
    </SummaryRow>;
}